﻿using Library.OrmEnterpriseEntity.TableInfo;
using System.Text;

namespace Library.OrmEnterpriseEntity {
	public sealed class DeleteQueryStatement : QueryStatement {
		internal DeleteQueryStatement(GlobalEntityView root)
			: this(root, null) {
		}
		internal DeleteQueryStatement(GlobalEntityView root, StringBuilder sqlTracking)
			: base(root) {
			SqlTracking = sqlTracking;
		}
		public override int Excute() {
			return Factor.DbContext.Instance.Context.ExcuteStatement(new DeleteStatementWrapper( this));

		}
		
		public DeleteQueryStatement Join(JoinStatement jointStatement) {
			jointStatement.Statement = this;
			jointStatement.Root = Root;
			Joints.Add(jointStatement);
			return this;
		}

		public DeleteQueryStatement Join(GlobalEntityView table, QueryExpBuilder joinExpr) {
			return Join(new JoinStatement(table, joinExpr));
		}

		public DeleteQueryStatement Join(GlobalEntityView table, DbColumnView first, QueryOperator queryOperator, DbColumnView second) {
			return Join(table, new QueryExpBuilder(first, queryOperator, second));
		}

		public DeleteQueryStatement Where(QueryExpBuilder whereExpression) {
			if (whereExpression != null) {
				whereExpression.Statement = this;
				foreach (var expressionBuilder in whereExpression.Builders) {
					expressionBuilder.Statement = this;
				}
				WhereExpression = whereExpression;
			}
			return this;
		}

	}
}
